package com.example.unitybletest;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.seventh.blecore.ByteArray;
import com.seventh.blecore.UnityCallBle;
import com.seventh.blecore.Utils;
import com.seventh.blecore.callback.NotifyCallback;
import com.seventh.blecore.callback.ResponseCallback;
import com.seventh.blecore.callback.SearchCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.UUID;

public class MainActivityCopy extends AppCompatActivity implements View.OnClickListener {
    private final UUID serviceUUID = UUID.fromString("0000fddd-0000-1000-8000-00805f9b34fb");
    private final UUID characterUUID = UUID.fromString("0000fdd1-0000-1000-8000-00805f9b34fb");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化一次
        UnityCallBle.getInstance(this).setPostUi(false);
        UnityCallBle.getInstance(this).setAndroidTestActivity(this);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_search).setOnClickListener(this);
        findViewById(R.id.btn_connect).setOnClickListener(this);
        findViewById(R.id.btn_notify).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_search) {
            search();
        } else if (v.getId() == R.id.btn_connect) {
            UnityCallBle.getInstance().disconnect(mMac);
        } else if (v.getId() == R.id.btn_notify) {
            notify(mMac);
        }
    }

    private String mMac = "F6:0D:0C:5E:A2:2B";

    private void search() {
        UnityCallBle.getInstance().searchByName(null, 5000, true, new SearchCallback() {
            @Override
            public void onSearchStarted() {
                Log.e("print", "onSearchStarted:" + Thread.currentThread());
            }

            @Override
            public void onDeviceFounded(@NonNull String name, @NonNull String mac, int rssi) {
                Log.e("print", "onDeviceFounded:" + Thread.currentThread() + " name->" + name + " mac->" + mac + " rssi->" + rssi);
            }

            @Override
            public void onSearchStopped(@NonNull String devicesJson, int indexOfDevice) {
                Utils.d(devicesJson);
                Log.e("print", "onSearchStopped:" + indexOfDevice);
                Log.e("print", "onSearchStopped:" + devicesJson);
                try {
                    JSONArray ja = new JSONArray(devicesJson);
                    for (int index = 0; index < ja.length(); index++) {
                        JSONObject jo = ja.getJSONObject(index);
                        if (jo.getString("name").contains("VRSEEN")) {
                            connect(jo.getString("mac"));
                            break;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onSearchError(int code, @NonNull String msg) {
                Log.e("print", "onSearchError:" + Thread.currentThread() + " code->" + code + ", msg->" + msg);

            }
        });
    }

    private void connect(String mac) {
        UnityCallBle.getInstance().connect(mac, new ResponseCallback() {
            @Override
            public void onResponse(@NonNull String mac, int code, @NonNull String msg) {
                Log.e("print", "onConnectResponse: code->" + code + ", msg" + msg);
                if (code == 0) {
                    MainActivityCopy.this.notify(mac);
                }
            }
        });
    }

    private void notify(String mac) {
        UnityCallBle.getInstance().notify(mac, serviceUUID.toString(), characterUUID.toString(), new NotifyCallback() {
            @Override
            public void onByteArray(String mac, @NonNull ByteArray byteArray) {
                Log.e("print", "onNotifyData:" + Thread.currentThread() + " byte->" + Utils.bytesToHex(byteArray.array));
            }

            @Override
            public void onResponse(@NonNull String mac, int code, @NonNull String msg) {
                Log.e("print", "onNotifyResponse: code->" + code + ", msg" + msg);
            }
        });
    }
}